package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.dao.DmcWatchDataSleepMapper;
import com.csun.cmny.provider.exceptions.DmcBizException;
import com.csun.cmny.provider.model.domain.DmcDevice;
import com.csun.cmny.provider.model.domain.DmcWatchDataSleep;
import com.csun.cmny.provider.model.dto.watch.WatchSleepQueryDto;
import com.csun.cmny.provider.model.vo.watch.DmcWatchDataVo;
import com.csun.cmny.provider.model.vo.watch.WatchSleepDayVo;
import com.csun.cmny.provider.service.DmcWatchDataService;
import com.csun.cmny.provider.service.DmcWatchDataSleepService;
import com.csun.cmny.util.PublicUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-01-24 15:16
 * @Description: the class dmc watch data sleep service impl
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class DmcWatchDataSleepServiceImpl extends BaseService<DmcWatchDataSleep>
        implements DmcWatchDataSleepService {

    @Resource
    private DmcWatchDataService dmcWatchDataService;

    @Resource
    private DmcWatchDataSleepMapper dmcWatchDataSleepMapper;

    /**
     * query hist sleep with day
     *
     * @param watchSleepQueryDto watch sleep query dto
     * @return the watch sleep day vo
     */
    @Override
    public WatchSleepDayVo queryHistSleepWithDay(WatchSleepQueryDto watchSleepQueryDto) {

        dmcWatchDataService.getWatchDataByDeviceId(watchSleepQueryDto.getDeviceId());

        WatchSleepDayVo watchSleepDayVo = new WatchSleepDayVo();

        DmcWatchDataSleep dmcWatchDataSleep = new DmcWatchDataSleep();
        dmcWatchDataSleep.setDeviceId(watchSleepQueryDto.getDeviceId());
        dmcWatchDataSleep.setSleepDate(watchSleepQueryDto.getDate().toString());
        dmcWatchDataSleep = dmcWatchDataSleepMapper.selectOne(dmcWatchDataSleep);
        if (PublicUtil.isEmpty(dmcWatchDataSleep)) {
            return null;
        } else {
            BeanUtils.copyProperties(dmcWatchDataSleep, watchSleepDayVo);
        }

        return watchSleepDayVo;
    }

    /**
     * 删除睡眠数据
     *
     * @param deviceId the device id
     * @return the int.
     */
    @Override
    public Integer removeDmcWatchDataSleep(Long deviceId) {

        DmcWatchDataSleep dmcWatchDataSleep = new DmcWatchDataSleep();
        dmcWatchDataSleep.setDeviceId(deviceId);

        return dmcWatchDataSleepMapper.delete(dmcWatchDataSleep);
    }
}
